function varargout = TLArithShiftDlg(action, varargin)

% TL_ARITHSHIFT_DLG manages the user interface for TargetLink Shift Arithmetic block
%
% SYNTAX dlgInfo = TLArithshiftDlg('GetDlgInfo');
%   gets dialog info of TargetLink Shift Arithmetic block
%
%   INPUT ARGUMENTS
%   -/-
%
%   OUTPUT ARGUMENTS
%   dlgInfo     dialog information contains pageNames etc.
%
% SYNTAX [sheetDims, ctrlPos] = TLArithshiftDlg('CtrlPositions', pageName, commonGeom, dlgInfo, sheetDims);
%   calculates the control positions of all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   commonGeom      common geometry data
%   dlgInfo         dialog information contains pageNames etc.
%   sheetDims       dimension of sheet
%
%   OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% SYNTAX dlgdata = TLArithshiftDlg('Create', pageName, dlgfig, dlgdata);
%   creates all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX [dlgdata,bModified] = TLArithshiftDlg('Manage', pageName, dlgfig, dlgdata, pageAction);
%   manages all actions of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageAction      modified control of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%   bModified       flag = 1, if dialog data are modified, =0 otherwise
%
% SYNTAX dlgdata = TLArithshiftDlg('Update', dlgfig, dlgdata, pageNum);
%   updates all controls of dialog page: <pageNum>
%
%   INPUT ARGUMENTS
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageNum         number of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX TLArithshiftDlg('Apply', dlgfig, dlgdata);
%   writes current blockdata to block and DD
%
%   INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
%   OUTPUT ARGUMENTS
%   - / -


% Author(s): D. Andoleit, T. Pietzsch
% $RCSfile: TLArithshiftDlg.m $
% $ProjectName: e:/ARC/Components/HostSW/SimulinkBlocksets/TLBlocksets/CommonBlockset/CommonBlockset_SimBlocksUI/Sources/MSrc/project.pj $
% $Revision: 1.5 $
% $Date: 2011/08/29 10:31:25MESZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.


% switch-case block for possible actions
switch lower(action)
    case 'getdlginfo'
        [varargout{1:2}] = FcnGetDlgInfo(varargin{1});
    case 'ctrlpositions'
        [varargout{1:2}] = FcnCtrlPositions(varargin{:});
    case 'create'
        [varargout{1}]   = FcnCreate(varargin{:});
    case 'manage'
        [varargout{1:2}] = FcnManage(varargin{:});
    case 'managesystemicons',
        [varargout{1:2}] = FcnManageSystemIcons(varargin{:});
    case 'update'
        [varargout{1}]   = FcnUpdate(varargin{:});
    case 'apply'
        [varargout{1:2}] = FcnApply(varargin{:});
    otherwise
        error(['Invalid action "' action '"']);
end


%-----------------------------------------------------------------------------------------
% FcnManageSystemIcons manage actions of toolbar icons
%
% INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%   action      action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageSystemIcons(dlgfig, dlgdata, action) %#ok

bModified = false;
switch action
    case 'EditCode'
        % look for associated production code file
        [codefile,blockstr] = get_codefile(dlgdata.model);
        if ~isempty(codefile)
            codeedit(codefile,blockstr);
        end
        
    case 'ViewCode'
        tl_create_links_to_model('ShowComputation',dlgdata.model, 'output');
        
    otherwise
        error('Invalid callback');
        
end % switch(action)


%--------------------------------------------------------------------------
% FcnGetDlgInfo gets dialog info of TargetLink shiftarithmetic block
%
% INPUT ARGUMENTS
%   hBlock      current block handle
%
% OUTPUT ARGUMENTS
%   dlgInfo     dialog information contains pageNames etc.
%   tlData      TargetLink block data
%--------------------------------------------------------------------------
function [dlgInfo, tlData] = FcnGetDlgInfo(hBlock)

dlgInfo = tl_get_dlginfodefaults('tlsim');
titleName = 'Shift Arithmetic';
pathinfo = tl_pathinfo(hBlock);
dlgInfo.title = sprintf('TargetLink %s: %s', titleName, pathinfo.shortDisplayName);
dlgInfo.pageNames = {...
    'Output & Logging',...
    'Documentation'};
tlData = get_tlcg_data(hBlock);


%--------------------------------------------------------------------------
% FcnCtrlPositions calculates the cotrol positions of all controls of
% dialog page: <pageName>
%
% INPUT ARGUMENTS
%   pageName        name of current page
%   commonGeom      common geometrie data
%   dlgInfo         dialog information contains pageNames etc.
%   sheetDims       dimension of sheet
%
% OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%--------------------------------------------------------------------------
function [sheetDims, ctrlPos] = FcnCtrlPositions(pageName, commonGeom, dlgInfo, sheetDims)  %#ok<INUSL>

% initialize output parameter
ctrlPos = [];
switch pageName
    case 'Output & Logging'
        [sheetDims,ctrlPos] = FcnCalcOutputControlPositions(commonGeom);
    case 'Documentation'
        bTblBlock = 0;
        bCodeComment = 1;
        [sheetDims, ctrlPos] = TLManageDocCtrls('ctrlpos', commonGeom, ...
            sheetDims, bCodeComment, bTblBlock);
end


%--------------------------------------------------------------------------
% FcnCreate creates all controls of dialog page: <pageName>
%
% INPUT ARGUMENTS
%   pageName    name of current page
%   dlgfig      current figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnCreate(pageName, dlgfig, dlgdata)

pageNum  = find(strcmp(pageName, dlgdata.pageNames));
switch pageName
    case 'Output & Logging'
        dlgdata = FcnCreateOutputControls(pageNum, dlgfig, dlgdata);
        
    case 'Documentation'
        dlgdata = TLManageDocCtrls( ...
            'create', pageNum, dlgfig, dlgdata);
end
% ctrlPos are not needed anymore
dlgdata.pageData{pageNum}.ctrlPos = [];


%--------------------------------------------------------------------------
% FcnManage manage all actions of dialog page: <pageName>
%
% INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          name of current page
%   dlgdata         dialog data
%   pageAction      name of calling control
%
% OUTPUT ARGUMENTS
%   dlgdata     dialog data
%   bModified   flag = 1, if dialog data are modified, = 0 otherwise
%--------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManage(pageName, dlgfig, dlgdata, pageAction)
bModified = false;
pageNum    = strmatch(pageName,dlgdata.pageNames,'exact');
switch pageName
    case 'Output & Logging'
        [dlgdata, bModified] = FcnManageOutputControls(pageNum, dlgfig, ...
            dlgdata,pageAction);
    case 'Documentation'
        [dlgdata, bModified] = TLManageDocCtrls('callback', ...
            pageNum, dlgfig, dlgdata, pageAction);
end

%--------------------------------------------------------------------------
% FcnUpdate updates all controls of dialog page: <pageNum>
%
% INPUT ARGUMENTS
%   dlgfig          name of current page
%   dlgdata         dialog data
%   pagenum         number of current tab
%
% OUTPUT ARGUMENTS
%   dlgdata         dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnUpdate(dlgfig, dlgdata, pageNum)
pageName  = dlgdata.pageNames{pageNum};
switch pageName
    case 'Output & Logging'
        dlgdata = FcnUpdateOutputControls(dlgfig,dlgdata,pageNum);
    case 'Documentation'
        TLManageDocCtrls('update', pageNum, dlgfig, dlgdata);
end


%--------------------------------------------------------------------------
% FcnApply writes current blockdata to block and DD
%
% INPUT ARGUMENTS
%   -/-
%
% OUTPUT ARGUMENTS
%   argout      return value is bContinue
%--------------------------------------------------------------------------
function [dlgdata, bClose] = FcnApply(dlgfig, dlgdata, bClose) %#ok<INUSL>
% ask for variable type, if apply-button was pressed after a new
% variable was entered in the variable field.
if ~isempty(dlgdata.data.output.variable)
    bExistOutputVar = logical(dsdd('Exist', dlgdata.data.output.variable, ...
        'Parent',       '/Pool/Variables', ...
        'objectKind',   'Variable'));
    if ~bExistOutputVar
        typeSelection = tl_dsdddlgif( 'SelectObject', ...
            'CurrentPath',  dlgdata.data.output.type, ...
            'ObjectKind',   'Typedef', ...
            'BlockType',    dlgdata.maskType, ...
            'BlockVar',     'output');
        
        if ~isempty(typeSelection)
            dlgdata.data.output.type = typeSelection;
            % create variable objects in data dictionary
            tl_export_blockdata_dd(dlgdata.data,'BlockType',dlgdata.maskType,'BlockHandle',dlgdata.model);
        end
    end
end
[dlgdata, bClose] = tl_apply_blockdata(dlgdata, bClose);

%-----------------------------------------------------------------------------------------
% FcnCalcOutputControlPositions calculates positions for all controls on output page
%
% INPUT ARGUMENTS
%   commonGeom      common geometrie information
%
% OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% HINT:
% Positions are calculated from the bottom left of the figure to avoid
% dependencies on figure size
%-----------------------------------------------------------------------------------------
function [sheetDims,ctrlPos] = FcnCalcOutputControlPositions(commonGeom)

% calculate positions of output controls
excludedCtrls = {'allscaling','inheritproperties', 'castoutput', 'width', ...
    'element', 'scaling', 'scalarExp', 'unit', 'type'};
[sheetDims, ctrlPosOutputFrame, colOutputFrame, colDelta, rowDelta] =  ...
    tl_calc_ctrls_positions(commonGeom, excludedCtrls);

% calculate positions of logging controls
excludedCtrls = {'autoscaling'};
[sheetDims, ctrlPos] = ...
    TLManageLoggingCtrls('calcCtrls', commonGeom, sheetDims, excludedCtrls);

% merge position structs
fieldNameList = fieldnames(ctrlPosOutputFrame);
fieldNameList = reshape(fieldNameList,1,length(fieldNameList));
for fieldName = fieldNameList,
    ctrlPos.(fieldName{1}) = ctrlPosOutputFrame.(fieldName{1});
end

% adapt box cordinates in consideration of missing unit controls
ctrlPos.descEdit(3) = ctrlPos.nameMenuButton(1) + ctrlPos.nameMenuButton(3) ...
    - ctrlPos.descEdit(1);

% adapt cordinates of logging box to output box
ctrlPos.samplesLabel(1) = colOutputFrame(4);
ctrlPos.samplesEdit(1)  = ctrlPos.samplesLabel(1) + ...
    ctrlPos.samplesLabel(3) + commonGeom.frameDelta;
ctrlPos.samplesEdit(3)  = ctrlPosOutputFrame.addressEdit(1) + ...
    ctrlPosOutputFrame.addressEdit(3) - ctrlPos.samplesEdit(1);

ctrlPos.logPopup(1) = colOutputFrame(2);
ctrlPos.logPopup(3) = colOutputFrame(4) - colOutputFrame(2) - colDelta/3;
ctrlPos.plotEdit(1) = colOutputFrame(2);
ctrlPos.plotEdit(3) = colOutputFrame(4) - colOutputFrame(2) - colDelta/3;
strings_and_styles = {...
    'Bit shifts:',                                          'text'; ...
    'External source',                                      'checkbox'; ...
    'Diagnostic for out-of-range shift value (MIL mode)',   'text'};
[shiftBitArithmeticLabelSize, ...
    externalSourceCheckboxSize, ...
    diagnosticLabelSize] = ...
    ds_get_uicontrol_sizes(commonGeom,strings_and_styles);

% recalculate group boxes' width
groupBoxWidth = ctrlPos.descEdit(1) + ctrlPos.descEdit(3) + commonGeom.frameSideEdgeBuffer;
ctrlPos.logplotGroupbox(3) = groupBoxWidth;
ctrlPos.tlcgoptGroupbox(3) = groupBoxWidth;
ctrlPos.shiftArithmeticGroupbox(3) = groupBoxWidth;
ctrlPos.tlcgoptGroupbox(2) = ctrlPos.tlcgoptGroupbox(2) + ctrlPos.logplotGroupbox(4) + commonGeom.textHeight;

% define an additional box for shiftArithmetic
ctrlPos.shiftArithmeticGroupbox(1) = ctrlPos.tlcgoptGroupbox(1);
ctrlPos.shiftArithmeticGroupbox(2) = ctrlPos.tlcgoptGroupbox(2) + ...
    ctrlPos.tlcgoptGroupbox(4) + commonGeom.textHeight;
ctrlPos.shiftArithmeticGroupbox(4) = 4*commonGeom.editHeight + 3*rowDelta + ...
    commonGeom.frameTopEdgeBuffer + commonGeom.frameBottomEdgeBuffer;

cy = commonGeom.frameBottomEdgeBuffer;
cx = colOutputFrame(1);
% protection checkbox
ctrlPos.protectionCheckbox = [cx, cy, ctrlPos.descEdit(3), externalSourceCheckboxSize(2)];

% diagnostic popup
cy = cy + commonGeom.editHeight + rowDelta;
ctrlPos.diagnosticLabel = [cx, cy, diagnosticLabelSize];
cx = ctrlPos.diagnosticLabel(1) + ctrlPos.diagnosticLabel(3) + colDelta;
w = colOutputFrame(4) + externalSourceCheckboxSize(1) -cx;
ctrlPos.diagnosticPopup = [cx, cy, w, externalSourceCheckboxSize(2)];

% shift bit direction label
cx = colOutputFrame(1);
cy = cy + commonGeom.editHeight + rowDelta;
ctrlPos.shiftBitDirectionLabel = [cx cy shiftBitArithmeticLabelSize];

% shift bit direction edit field
cx = colOutputFrame(2);
ctrlPos.shiftBitDirectionPopup = [cx, cy, ctrlPos.logPopup(3:4)];

cy = cy + commonGeom.editHeight + rowDelta;
% shift bit arithmetic label
cx = colOutputFrame(1);
ctrlPos.shiftBitArithmeticLabel = [cx cy shiftBitArithmeticLabelSize];

% shift bit arithmetic edit field
cx = colOutputFrame(2);
ctrlPos.shiftBitArithmeticEdit = [cx, cy, ctrlPos.logPopup(3:4)];

% shift bit external source checkbox
cx = colOutputFrame(4);
ctrlPos.externalSourceCheckbox = [cx, cy, externalSourceCheckboxSize];

% calculate sheet Dimensions
width = ctrlPos.tlcgoptGroupbox(3) ...
    + 2 * commonGeom.frameSideEdgeBuffer;
height = ctrlPos.logplotGroupbox(4) ...
    + ctrlPos.tlcgoptGroupbox(4) ...
    + ctrlPos.shiftArithmeticGroupbox(4) ...
    + 3 * commonGeom.textHeight ...
    + commonGeom.sheetBottomEdgeBuffer;

sheetDims = [width height];


%-----------------------------------------------------------------------------------------
% FcnCreateOutputControls creates the figure window
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateOutputControls(pageNum,dlgfig,dlgdata)
% create logging controls
dlgdata = TLManageLoggingCtrls('create', pageNum, dlgfig, dlgdata);

Children = dlgdata.pageData{pageNum}.children;
ctrlPos  = dlgdata.pageData{pageNum}.ctrlPos;
pageName = dlgdata.pageNames{pageNum};
% create shift arithmetic group box
Children.shiftArithmeticGroupbox = uipanel( ...
    'Parent',            dlgfig, ...
    'Title',            {'Shift Arithmetic'}, ...
    'Units',            'pixels', ...
    'Position',          ctrlPos.shiftArithmeticGroupbox);

% create bit shift arithmetic controls
Children.shiftBitArithmeticLabel = uicontrol( ...
    'Parent',           Children.shiftArithmeticGroupbox, ...
    'Style',            'text', ...
    'String',           'Bit-shifts:', ...
    'Position',         ctrlPos.shiftBitArithmeticLabel);

% Tag is needed for automatic dev test
Children.shiftBitArithmeticEdit = uicontrol( ...
    'Tag',              'shiftBitArithmeticEdit', ...
    'Parent',           Children.shiftArithmeticGroupbox, ...
    'BackgroundColor',  'w', ...
    'Style',            'edit', ...
    'String',           '0', ...
    'Position',         ctrlPos.shiftBitArithmeticEdit, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''shiftBitArithmeticEdit'')',pageName));

% create bit shift direction controls
Children.shiftBitDirectionLabel = uicontrol( ...
    'Parent',           Children.shiftArithmeticGroupbox, ...
    'Style',            'text', ...
    'String',           'Direction:', ...
    'Position',         ctrlPos.shiftBitDirectionLabel);

Children.shiftBitDirectionPopup = uicontrol( ...
    'Parent',           Children.shiftArithmeticGroupbox, ...
    'BackgroundColor',  'w', ...
    'Style',            'Popupmenu', ...
    'Position',         ctrlPos.shiftBitDirectionPopup, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''shiftBitDirectionPopup'')',pageName));

% create bit shift external source checkbox
Children.externalSourceCheckbox = uicontrol( ...
    'Parent',           Children.shiftArithmeticGroupbox, ...
    'Style',            'checkbox', ...
    'String',           'External source', ...
    'Position',         ctrlPos.externalSourceCheckbox, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''externalSourceCheckbox'')',pageName));

% create bit shift external source checkbox
Children.diagnosticLabel = uicontrol( ...
    'Parent',           Children.shiftArithmeticGroupbox, ...
    'Style',            'text', ...
    'String',           'Diagnostic for out-of-range shift value (MIL mode):', ...
    'Position',         ctrlPos.diagnosticLabel);

Children.diagnosticPopup = uicontrol( ...
    'Parent',           Children.shiftArithmeticGroupbox, ...
    'BackgroundColor',  'w', ...
    'Style',            'Popupmenu', ...
    'Position',         ctrlPos.diagnosticPopup, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''diagnosticPopup'')',pageName));

% create bit shift external source checkbox
Children.protectionCheckbox = uicontrol( ...
    'Parent',           Children.shiftArithmeticGroupbox, ...
    'Style',            'checkbox', ...
    'String',           'Protect against out-of-range shift value (production code)', ...
    'Position',         ctrlPos.protectionCheckbox, ...
    'Callback',           sprintf('tldlg(''%s'',gcbf,''protectionCheckbox'')',pageName));

dlgdata.pageData{pageNum}.children = Children;

% create standard variable controls
dlgdata = TLManageVariableCtrls('create', pageNum, dlgfig, dlgdata);
% modify panel title
set(dlgdata.pageData{pageNum}.children.tlcgoptGroupbox, 'Title', 'Output');


%-----------------------------------------------------------------------------------------
% FcnManageOutputControls manages control actions of output page
%
% INPUT ARGUMENTS
%   pageNum         number of current page
%   dlgfig          handle for dialog figure
%   dlgdata         dialog data
%   pageAction      action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%   bModified       flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageOutputControls(pageNum,dlgfig,dlgdata,action)

Children = dlgdata.pageData{pageNum}.children;
switch action
    case 'shiftBitArithmeticEdit'
        shiftbitsStr = get(Children.shiftBitArithmeticEdit, 'String');
        if ~isempty(shiftbitsStr)
            if strcmp(shiftbitsStr, dlgdata.data.bitshiftnumber)
                bModified = false;
            else
                dlgdata.data.bitshiftnumber = shiftbitsStr;
                bModified = true;
            end
        else
            % Bit-shifts must not be empty
            beep;
            bModified = true;
        end
        
    case 'shiftBitDirectionPopup'
        dlgdata.data.bitshiftdirection = get(Children.shiftBitDirectionPopup, 'Value');
        bModified = true;
        
    case 'externalSourceCheckbox'
        switch get(Children.externalSourceCheckbox, 'Value')
            case 0
                dlgdata.data.bitshiftnumbersource = 1;
            case 1
                dlgdata.data.bitshiftnumbersource = 2;
        end
        bModified = true;
        
    case 'diagnosticPopup'
        dlgdata.data.diagnosticforoorshift = get(Children.diagnosticPopup, 'Value');
        bModified = true;
        
    case 'protectionCheckbox'
        dlgdata.data.checkoorbitshift = get(Children.protectionCheckbox, 'Value');
        bModified = true;
        
    case {'logPopup', 'plotEdit', 'samplesEdit'}
        [dlgdata, bModified] = ...
            TLManageLoggingCtrls('manage', pageNum, dlgdata, ...
            action, 'logdata');
        
    otherwise
        [dlgdata, bModified] = TLManageVariableCtrls( ...
            'manage', pageNum, dlgfig, dlgdata, action, 'output');
end

%-----------------------------------------------------------------------------------------
% FcnUpdateOutputControls updates output controls
%
% INPUT ARGUMENTS
%   dlgfig          figure handle of dialog
%   dlgdata         dialog data
%   pageNum         page Number
%
% OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnUpdateOutputControls(dlgfig,dlgdata,pageNum)

% update all controls
dlgdata =   TLManageVariableCtrls('update', pageNum, dlgdata, 'output');
dlgdata =   TLManageLoggingCtrls('update', pageNum, dlgdata,'logdata','output');
Children = dlgdata.pageData{pageNum}.children;

% if field "variable" is not empty, check if it has already an error. If it has
% no error, check if it has the right data type in the DD.
if ~strcmp(dlgdata.data.output.variable, '')
    % check if variable edit has already an error
    userData = get(Children.varEdit, 'UserData');
    errorCode = userData.ctrlStatus.iserror;
    % if variable has no error, check if its type is integer in DD
    if errorCode==0
        variableFlag = tl_dsdddlgif( 'IsCompatible', ...
            'CurrentPath',  dlgdata.data.output.variable, ...
            'ObjectKind',   'Variable', ...
            'BlockType',    dlgdata.maskType, ...
            'BlockVar',     'output');
        
        % if variable exists in DD and has a wrong data type
        if variableFlag == 2
            userData.ctrlStatus.iserror = 1;
            set(Children.varEdit, 'UserData', userData);
            % set new error code
        end
    end
end

% get enums
enums = tl_manage_blockset('GetEnums');

% out-of-range check controls
userData = get(Children.diagnosticPopup, 'UserData');
% get DiagnosticForOORShiftEnum
diagnosticEnum = enums(strcmp({enums.name},'DiagnosticForOORShiftEnum'));
userData.ctrlStatus = struct(   'enable',   'on', ...
    'value',    dlgdata.data.diagnosticforoorshift, ...
    'string',   {diagnosticEnum.strings});
set(Children.diagnosticPopup, 'UserData', userData);

% protection checkbox
userData = get(Children.protectionCheckbox, 'UserData');
userData.ctrlStatus.enable = flag2str(dlgdata.data.bitshiftnumbersource == 2);
userData.ctrlStatus.value  = dlgdata.data.checkoorbitshift;
set(Children.protectionCheckbox, 'UserData', userData);

% bit shift direction popup
userData = get(Children.shiftBitDirectionPopup, 'UserData');
% get BitShiftDirectionEnum
directionEnum = enums(strcmp({enums.name},'BitShiftDirectionEnum'));
userData.ctrlStatus = struct(   'enable',   'on', ...
    'value',    dlgdata.data.bitshiftdirection, ...
    'string',   {directionEnum.strings});
set(Children.shiftBitDirectionPopup, 'UserData', userData);

% external source checkbox
userData = get(Children.externalSourceCheckbox, 'UserData');
userData.ctrlStatus = struct('enable', 'on', 'value', dlgdata.data.bitshiftnumbersource == 2);
set(Children.externalSourceCheckbox, 'UserData', userData);

% check bitindex edit
userData = get(Children.shiftBitArithmeticEdit, 'UserData');
userData.ctrlStatus.iserror = false;
value = str2num(dlgdata.data.bitshiftnumber); %#ok<ST2NM>
if ~isempty(value)
    if  ~isreal(value) || any(isnan(value)) || any(round(value) ~= value)
        userData.ctrlStatus.iserror = true;
    end
end
userData.ctrlStatus.enable = flag2str(dlgdata.data.bitshiftnumbersource == 1);
userData.ctrlStatus.string = dlgdata.data.bitshiftnumber;
set(Children.shiftBitArithmeticEdit, 'UserData', userData);
% set controls
TLManageVariableCtrls('setCtrls', pageNum, dlgfig, dlgdata);
TLManageLoggingCtrls('setCtrls',pageNum, dlgfig, dlgdata);
shiftArithmeticControls = { ...
    'shiftBitArithmeticEdit', ...
    'shiftBitDirectionPopup', ...
    'externalSourceCheckbox', ...
    'diagnosticPopup', ...
    'protectionCheckbox'};
TLSetCtrlsStatus(dlgfig, Children, shiftArithmeticControls);
